package baseclasses;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.content.Context;
import database.CourseDAO;

public class Assignment {
	
	//static final string used for passing assignment IDs to activities through intents
	public static final String ASSIGNMENT_ID = "ASSIGNMENT_ID";

	private int id;
	private int courseId;
	private String name, courseName = null;
	private String description = null;
	private Calendar dueDate;
	private long timeUntilDue = -1;	//default alarm receiver value
	private String timeUntilDueString = null;
	private boolean completed = false;

	public Assignment(){}	//default constructor

	public Assignment(int courseId, String name, String description,
			Calendar dueDate, boolean completed) {	//Constructor used if assignment is created from the AddAssignment class
		super();
		this.courseId = courseId;
		this.name = name;
		this.description = description;
		this.dueDate = dueDate;
		this.completed = completed;
	}

	public Assignment(int id, int courseId, String name, String description,
			Calendar dueDate, boolean completed) { //Constructor used if assignment is created from EditAssignment class
		super();
		this.id = id;
		this.courseId = courseId;
		this.name = name;
		this.description = description;
		this.dueDate = dueDate;
		this.completed = completed;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getCourseId() {
		return courseId;
	}

	public void setCourseId(int courseId) {
		this.courseId = courseId;
		this.courseName = null;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}
	
	public void setCompleted( boolean completed )
	{
		this.completed = completed;
	}
	
	public void setCompleted( int i )	//used for assignment completed mark
	{
		if( i > 0 )
		{
			this.completed = true;
		}
		else
		{
			this.completed = false; 	//-1 is default not set
		}
	}
	
	public boolean isCompleted()
	{
		return completed;
	}

	public String getShortDescription(){
		if(description.length()>30){
			return description.substring(0, 27)+"..."; //first 26 characters in the description string
		}
		else{
			return description;
		}
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Calendar getDueDate() {
		return dueDate;
	}

	public long getDueDateLong(){
		return dueDate.getTimeInMillis();
	}

	public String getDueDateString(String dateformat){
		SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
		return sdf.format(dueDate.getTimeInMillis());
	}

	public static String getDueDateString(Calendar dueDate, String dateformat){
		SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
		return sdf.format(dueDate.getTimeInMillis());
	}

	public void setDueDate(Calendar dueDate) {
		this.dueDate = dueDate;
		this.timeUntilDue = -1;
		this.timeUntilDueString=null;
	}

	public void setDueDate(long time) {
		dueDate = Calendar.getInstance();
		dueDate.setTimeInMillis(time);
		this.timeUntilDue = -1;
		this.timeUntilDueString=null;
	}

	public long getTimeUntilDue() {
		//if timeUntilDue is not set (-1) then set it
		if(timeUntilDue==-1){
			timeUntilDue = dueDate.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
		}
		return timeUntilDue;
	}

	//return string representation of time until due
	public String getTimeUntilDueString() {
 
		final long DAYS = 1000*60*60*24; //millis * seconds * minutes * hours
		Calendar calendar = Calendar.getInstance();


		//if timeUntilDue is not set (-1) then set it
		if(timeUntilDue==-1){
			timeUntilDue = dueDate.getTimeInMillis() - calendar.getTimeInMillis();
			timeUntilDueString = null;
		}

		if(timeUntilDueString==null){

			//if the due date has past return "Overdue"
			if(timeUntilDue<=0){
				timeUntilDueString = "Overdue";
			}
			//if due date is less than 24 hrs return time
			else if(timeUntilDue<=DAYS){
				SimpleDateFormat sdf = null; //get new simpleDateFormat instance
				if(dueDate.get(Calendar.DAY_OF_WEEK)==calendar.get(Calendar.DAY_OF_WEEK)){
					sdf = new SimpleDateFormat("hh:mm a");	
				}
				else{
					sdf = new SimpleDateFormat("EEE hh:mm a");
				}
				timeUntilDueString = sdf.format(dueDate.getTimeInMillis());
			}

			//if due date is less than three days return days
			else if(timeUntilDue<=DAYS*7){
				SimpleDateFormat sdf = new SimpleDateFormat("EEE hh:mm a");
				timeUntilDueString = sdf.format(dueDate.getTimeInMillis());
			}

			//else return the date
			else{
				timeUntilDueString = getDueDateString("MMM dd ");
			}
		}

		return timeUntilDueString;

	}

	public void setTimeUntilDue(long timeUntilDue) {
		this.timeUntilDue = timeUntilDue;
	}

	public void setCourseName(String courseName){
		this.courseName = courseName;
	}

	public String getCourseName(Context context){
		if(courseName == null){
			courseName = CourseDAO.getCourseNameById(context, courseId);
			//get the courseName by id from CourseDAO in the database package
		}
		return courseName;
	}

	@Override
	public String toString(){
		return this.name;
	}


}
